<?php
/**
 * 自定义表单管理
 */
namespace Admin\Controller;
use Admin\BaseController;

use Common\Model\FormModel;
use Common\Model\RedpackModel;
use Common\Model\UtilsModel;

class FormController extends BaseController{

	/**
	 * 文章列表页
	 * Author: shijy
	 */
	public function index(){

		$pageSize	=I('post.limit',10,'int');
		$page		=I('post.p',1,'int');




		$list=FormModel::getList($page,$pageSize,0);
		fetchAjaxPager($this->smarty,$list,$pageSize);

		$this->smarty->assign('auditStatus',RedpackModel::PACK_STATUS_ONLINE);

		if(IS_AJAX){
			$this->smarty->assign('list',$list['data']);
			$listHtml=$this->smarty->fetch('Form/set_list.html');


			$pagerHtml=$this->smarty->fetch('Public/page_ajax.html');
			$this->ajaxReturn(array(
				'status'=>0,
				'listHTML'=>$listHtml,
				'pagerHTML'=>$pagerHtml
			));
		}else{
			$this->smarty->assign('list',$list['data']);
			$this->smarty->display('Form/set.html');
		}

	}


	/**
	 * 导出表单填写数据
	 */
	public function exportRecord(){
		$beginTime	=I('get.startDate');
		$endTime	=I('get.endDate');
		$fid		=I('get.id',0,'int');
		$nickname	=I('get.nickname');

		$formDetail = FormModel::getFormInstance()->where('fid='.$fid)->find();
		if(!$formDetail){
			$this->ajaxReturn(array(
				'status'=>1,'msg'=>'指定表单不存在'
			));
		}

		$fields=explode(';',$formDetail['content']);
		$paramArray=array('用户昵称','填写时间');
		foreach($fields as $one){
			list($fname,$ftypeParam,$must,$append)=explode('+',$one);
			$paramArray[]=$fname;

		}
		unset($fields);


		$beginTS	=strtotime($beginTime);
		$endTs		=strtotime($endTime);
		if($endTs==$beginTS && $beginTS>0){
			$endTs +=UtilsModel::ONE_DAY;
		}
		$list=FormModel::getRecordList(0,PHP_INT_MAX,$fid,$nickname,$beginTS,$endTs);

		$data=array();
		foreach($list['data'] as $item){
			$tmp=explode(';',$item['data']);
			array_unshift($tmp,date('Y-m-d H:i:s',$item['addTime']));
			array_unshift($tmp,$item['nickname']);
			$data[]=$tmp;
		}
		exportExcel($data, $paramArray,$formDetail['title']);

	}

	/**
	 * 红包记录
	 */
	public function recordList(){

		$export		=I('get.export',0,'intval');
		if($export){
			$this->redirect(U('exportRecord'),$_GET);
		}

		$beginTime	=I('post.startDate');
		$endTime	=I('post.endDate');

		$fid		=I('get.id',0,'int');
		if(!$fid){
			$fid	=I('post.id',0,'int');
		}else{
			$this->smarty->assign('fid',$fid);
		}

		$pageSize	=I('post.limit',10,'int');
		$page		=I('post.p',1,'int');
		$nickname	=I('post.nickname');


		$formDetail = FormModel::getFormInstance()->where('fid='.$fid)->find();
		if(!$formDetail){
			$this->ajaxReturn(array(
				'status'=>1,'msg'=>'指定表单不存在'
			));
		}

		$fields=explode(';',$formDetail['content']);
		$paramArray=array();
		foreach($fields as $one){
			list($fname,$ftypeParam,$must,$append)=explode('+',$one);
			$paramArray[]=$fname;

		}
		unset($fields);
		$this->smarty->assign('param',$paramArray);

		$beginTS	=strtotime($beginTime);
		$endTs		=strtotime($endTime);
		if($endTs==$beginTS && $beginTS>0){
			$endTs +=UtilsModel::ONE_DAY;
		}

		$list=FormModel::getRecordList($page,$pageSize,$fid,$nickname,$beginTS,$endTs);
		fetchAjaxPager($this->smarty,$list,$pageSize);
		if(IS_AJAX){
			$this->smarty->assign('list',$list['data']);
			$listHtml=$this->smarty->fetch('Form/record_list.html');
			$pagerHtml=$this->smarty->fetch('Public/page_ajax.html');
			$this->ajaxReturn(array(
				'status'=>0,
				'listHTML'=>$listHtml,
				'pagerHTML'=>$pagerHtml
			));

		}else{
			$this->smarty->assign('formKv',FormModel::getFormKV());
			$this->smarty->assign('list',$list['data']);
			$this->smarty->display('Form/record.html');
		}

	}

	//活动添加
	public function add(){
		$this->smarty->assign('fieldKv',FormModel::getFieldType());

		$this->smarty->assign('FIELD_SINGLE',FormModel::FIELD_SINGLE);
		$this->smarty->assign('FIELD_MULTI',FormModel::FIELD_MULTI);
		$this->smarty->display('Form/edit.html');
	}


	public function edit(){
		$id=I('get.id',0,'int');
		$detail=FormModel::getFormInstance()->where('fid='.$id)->find();


		$this->smarty->assign('id',$id);
		$this->smarty->assign('title',$detail['title']);
		$this->smarty->assign('mem',$detail['mem']);
		$this->smarty->assign('mustFollow',$detail['mustFollow']);

		$fields=explode(';',$detail['content']);
		$paramArray=array();
		foreach($fields as $one){
			list($fname,$ftypeParam,$must,$append)=explode('+',$one);
			list($ftype,$fparam)=explode(':',$ftypeParam);
			$paramArray[]=array(
				'fname'=>$fname,
				'ftype'=>$ftype,
				'fparam'=>$fparam,
				'must'=>$must,
				'append'=>$append
			);
		}
		unset($fields);

		$this->smarty->assign('params',$paramArray);

		$this->smarty->assign('fieldKv',FormModel::getFieldType());
		$this->smarty->assign('FIELD_SINGLE',FormModel::FIELD_SINGLE);
		$this->smarty->assign('FIELD_MULTI',FormModel::FIELD_MULTI);

		$this->smarty->display('Form/edit.html');
	}

	//资讯添加或修改
	public function update(){
		$id=I('post.id',0,'int');

		$title=I('post.title','');
		$mem=I('post.mem',null);

		$fname		= I('post.fname');
		$ftype		= I('post.ftype');
		$fparam		= I('post.fparam');
		$must		= I('post.must');
		$append		= I('post.append');


		$fields=array();
		foreach($fname as $k=>$one){
			$fields[]=$fname[$k].'+'.$ftype[$k].':'.$fparam[$k].'+'.$must[$k].'+'.$append[$k];
		}
		if(count($fields)<1){
			$this->ajaxReturn(array('status'=>1,'msg'=>'请设置字段'));
		}
		$content=implode(';',$fields);


		$setData=array(
			'title'=>$title,
			'content'=>$content,
			'mem'=>$mem,
			'addTime'=>time(),
		);
		if ($id==0){//添加
			$result=FormModel::getFormInstance()->add($setData);
		}else{
			$result=FormModel::getFormInstance()->where("fid={$id}")->save($setData);
		}

		if ($result==false){
			$this->ajaxReturn(array('status'=>1,'msg'=>'保存失败'));
		}
		$this->ajaxReturn(array('status'=>0,'msg'=>'保存成功'));
	}



	/**
	 * 导出表单填写数据
	 */
	public function exportBirth(){
		$month		=I('get.month',0,'intval');
		$list=FormModel::getBirthList(0,PHP_INT_MAX,$month);
		$fileName="丸宝生日清单";
		if($month>0){
			$fileName .="_{$month}月生日";
		}
		$fileName .='_'.date('Y-m-d');
		exportExcel($list['data'], array('用户编号','家长','电话','地址','宝贝性别','宝贝昵称','宝贝生日','报名微信'),$fileName);

	}
	public function birthList(){

		$export		=I('get.export',0,'intval');
		if($export){
			$this->redirect(U('exportBirth'),$_GET);
		}

		$pageSize	=I('post.limit',10,'int');
		$page		=I('post.p',1,'int');
		$month		=I('post.month');



		$list=FormModel::getBirthList($page,$pageSize,$month);
		fetchAjaxPager($this->smarty,$list,$pageSize);
		if(IS_AJAX){
			$this->smarty->assign('list',$list['data']);
			$listHtml=$this->smarty->fetch('Form/birth_list.html');
			$pagerHtml=$this->smarty->fetch('Public/page_ajax.html');
			$this->ajaxReturn(array(
				'status'=>0,
				'listHTML'=>$listHtml,
				'pagerHTML'=>$pagerHtml
			));
		}else{
			$this->smarty->assign('list',$list['data']);
			$this->smarty->display('Form/birth.html');
		}
	}

}
